<template>
  <div class="app">
    <div class="room-banner">
      <div class="room-title">热门房间</div>
      <div class="room-search">
        <input
          class="search-text"
          type="text"
          v-model="searchValue"
          @focus="searchFocused"
          @keydown="searchKeydown"
          placeholder="输入房间ID或名称搜索"
        />
        <button class="search-button" @click="getRoomList()">
          <i class="el-icon-search">搜索房间</i>
        </button>
      </div>
    </div>
    <div class="room-list">
      <div class="room-online">
        <template v-for="item in roomList" :key="item.room_id">
          <div class="room-item" v-on:dblclick="joinRoom(item)">
            <div class="room-top">
              <div class="room-image">
                <el-image :src="item.room_logo"></el-image>
                <div class="room_id">ID:{{ item.room_id }}</div>
              </div>
              <div class="room-info">
                <div class="room-name">{{ item.room_name }}</div>
                <div class="user-name">
                  <span>房主</span>{{ item.user.user_name }}
                </div>
                <div class="room-bottom">
                  {{ item.room_notice }}
                </div>
              </div>
            </div>
          </div>
        </template>
      </div>
      <div class="room-history">
        <el-empty description="暂无足迹"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roomList: [],
      room: false,
      password: "",
      searchValue: "",
    };
  },
  watch: {
    "$store.state.nowApp.key"() {
      if (this.$store.state.nowApp.key === "room_list") {
        this.getRoomList();
      }
    },
  },
  created() {
    this.$emit("api", {
      event: "setWindowTitle",
      title: " ",
    });
    this.getRoomList();
  },
  methods: {
    searchKeydown(e) {
      if (e.key === "Enter") {
        this.getRoomList();
      }
    },
    getRoomList() {
      this.request({
        url: "room/list",
        method: "POST",
        data: {
          keyword: this.searchValue,
        },
        success: (res) => {
          this.roomList = res.data;
        },
      });
    },
    joinRoom(room) {
      this.$emit("api", {
        event: "openApp",
        app: "room_main",
        data: JSON.parse(JSON.stringify(room)),
      });
    },
    searchFocused() {
      this.searchValue = "";
      this.getRoomList();
    },
  },
};
</script>
<style scoped lang="scss">
.app {
  display: flex;
  flex: 1;
  width: 100%;
  color: #333;
  text-shadow: none;
  background-color: #eee;
  flex-direction: column;
  .room-banner {
    background: #333;
    width: 100%;
    height: 60px;
    display: flex;
    flex-direction: row;
    align-items: center;
    .room-title {
      flex: 1;
      color: white;
      font-size: 24px;
      font-weight: 300;
      padding-left: 20px;
    }
    .room-search {
      width: 300px;
      padding-right: 5px;
      display: flex;
      flex-direction: row;
      .search-text {
        border: 1px solid #666;
        background-color: transparent;
        color: white;
        border-radius: 5px;
        border-right: none;
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
        padding: 8px 12px;
        font-size: 14px;
        color: #aaa;
        transition: color 0.5s;
      }
      .search-text:focus {
        color: #fff;
      }
      .search-button {
        border: 1px solid #666;
        background-color: transparent;
        color: #aaa;
        border-radius: 5px;
        border-top-left-radius: 0;
        border-bottom-left-radius: 0;
        padding: 8px 12px;
        font-size: 14px;
        cursor: pointer;
        transition: color 0.5s;
      }
      .search-button:hover {
        color: #fff;
      }
    }
  }
  .room-list {
    display: flex;
    flex: 1;
    flex-direction: row;
    .room-online {
      display: flex;
      flex-direction: column;
      flex: 1;
      flex-grow: 1;
      background-color: #fff;
      .room-item {
        background: white;
        display: flex;
        flex-direction: column;
        position: relative;
        cursor: pointer;
        transition: background-color 0.5s;
        border-bottom: 1px solid #eee;
        .room-top {
          display: flex;
          flex-direction: row;

          .room-image {
            width: 80px;
            height: 80px;
            margin: 10px;

            .el-image {
              border-radius: 10px;
            }
          }

          .room_id {
            position: absolute;
            right: 10px;
            top: 10px;
            font-size: 12px;
            background: #ccc;
            color: #fff;
            padding: 2px 5px;
            border-radius: 3px;
            font-weight: 300;
          }

          .room-name {
            font-size: 16px;
            color: #111;
          }

          .room-info {
            padding: 10px 10px 10px 0px;
            flex: 1;
            width: 0;
          }

          .user-name {
            font-size: 14px;
            color: #999;
            margin-top: 5px;
            span {
              background: #ccc;
              color: white;
              font-size: 12px;
              padding: 1px 3px;
              margin-right: 5px;
              font-weight: 300;
              border-radius: 3px;
            }
          }

          .room-info * {
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            width: 100%;
          }
          .room-bottom {
            font-size: 12px;
            color: #aaa;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            margin-top: 10px;
          }
        }
      }
      .room-item:hover {
        background-color: #eee;
      }
    }
    .room-history {
      width: 300px;
      background-color: white;
      margin-left: 5px;
      position: relative;
      .el-empty {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
      }
    }
  }
}
</style>